// pages/order/order.js
const ORD = require("../../interfaces/order")
const VEH = require("../../interfaces/vehicles")
const IFS = require("../../interfaces/interface")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderList: [
      {
          "front_cars": 17,
          "id": "27",
          "user_id": "48",
          "car_id": "12345",
          "mode": "fast",
          "apply_kwh": 28,
          "charge_kwh": 38,
          "state": "cupidatat veniam esse",
          "charge_price": 54,
          "service_price": 53,
          "fee": 80,
          "create_time": "nisi quis ut sed amet",
          "dispatch_time": "ullamco sit",
          "charge_id": "94",
          "start_time": "veniam dolore sint",
          "finish_time": "sunt Lorem adipisicing",
          "number": 99
      },
      {
          "front_cars": 16,
          "id": "76",
          "user_id": "99",
          "car_id": "11111",
          "mode": "slow",
          "apply_kwh": 73,
          "charge_kwh": 96,
          "state": "irure",
          "charge_price": 92,
          "service_price": 1,
          "fee": 39,
          "create_time": "mollit eiusmod deserunt",
          "dispatch_time": "Duis eiusmod",
          "charge_id": "86",
          "start_time": "Excepteur ad laboris tempor dolore",
          "finish_time": "sunt aute",
          "number": 90
      }
  ],
  vehicleArray: [
    {
      car_id: "12345",
      name: "bmw",
      power_capacity: 12,
      power_current: 6
    },
    {
      car_id: "11111",
      name: "benz",
      power_capacity: 13,
      power_current: 7
    }
  ],

  searchCarName: "",
  searchDate: "",
  oriList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      orderList: ORD.getAllOrders(),
      vehicleArray: VEH.getAllVehicles()
    })

    var newList = this.data.orderList
    newList.forEach(item => {
      item.fee.toFixed(2)
      var tar_carID = item.car_id
      var tar_carName = ""
      this.data.vehicleArray.forEach(vehicle => {
        if (vehicle.car_id == tar_carID){
          tar_carName = vehicle.name
        }
      })
      item["carName"] = tar_carName
    });
    this.setData({
      oriList: newList,
      orderList: newList
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  rowTapped:function(e){
    let id=e.currentTarget.id
    wx.navigateTo({
      url: "./details/details?id="+id,
    })
  },

  inputCarName(e) {
    this.setData({
      searchCarName: e.detail.value.trim(),
    });
  },

  inputDate(e) {
    this.setData({
      searchDate: e.detail.value.trim(),
    });
  },
  onSearch() {
    this.setData({
      orderList: this.data.oriList
    })
    var newList1 = []
    var newList2 = []
    if (this.data.searchCarName != "")
    {
      this.data.orderList.forEach(item => {
        if (item.carName == this.data.searchCarName)
        {
          newList1.push(item)
        }
      });
    }
    else
    {
      newList1 = this.data.orderList
    }
    console.log(newList1)
    if (this.data.searchDate != "")
    {
      newList1.forEach(item => {
        if (item.start_time.includes(this.data.searchDate) || item.finish_time.includes(this.data.searchDate))
        {
          newList2.push(item)
        }
      });
    }
    else
    {
      newList2 = newList1
    }
    console.log(newList2)
    this.setData({
      orderList: newList2
    })
  }
})